<?php

class FoodPlaceDetails extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);

		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"]." - Food Place Details";
			
			//$this->infoHighlights->setAttribute("class", "detail");
			$this->infoCuisine->setAttribute("class", "cuisine");
			$this->infoMRT->setAttribute("class", "bus");
			$this->infoPrice->setAttribute("class", "price");
			$this->infoLocation->setAttribute("class", "location");
			$this->infoContact->setAttribute("class", "call");
			$this->infoOperatingHours->setAttribute("class", "open");
			
			$this->txtFoodPlaceID->Value = ($this->Request->contains('fid')) ? TPropertyValue::ensureInteger($this->Request['fid']) : 0;	
			$foodPlaceRecord = FoodPlaceRecord::finder()->findByPk($this->txtFoodPlaceID->Value);
			if ($foodPlaceRecord instanceof FoodPlaceRecord)
			{
				$this->lblFoodPlaceName->Text=$foodPlaceRecord->Name;
				
				//$this->infoHighlights->Text = $foodPlaceRecord->Highlights;
				$this->lblHighlights->Text = $foodPlaceRecord->Highlights;				
				
				// cuisine
				$foodPlaceCuisine = "";
				$criteria = new TActiveRecordCriteria;
				$criteria->Condition = "food_place_id = " . $foodPlaceRecord->ID;
				$items = FoodPlaceCuisineRecord::finder()->findAll($criteria);					
				foreach ($items as $foodPlaceCuisineRecord) 
				{
					$cuisine = CuisineRecord::finder()->findByPk($foodPlaceCuisineRecord->CuisineID);
					$foodPlaceCuisine .= $cuisine->Cuisine . ", ";																														
				} 				
				$foodPlaceCuisine = substr($foodPlaceCuisine,0,strlen($foodPlaceCuisine)-2);
				$this->infoCuisine->Text = $foodPlaceCuisine;
			
				$this->infoLocation->Text = $foodPlaceRecord->Location;
				$this->infoOperatingHours->Text = nl2br($foodPlaceRecord->OperatingHours);
					
				if ($foodPlaceRecord->NearestMRT > 0) {
					$mrt = MRTRecord::finder()->findByPk($foodPlaceRecord->NearestMRT);				
					$this->infoMRT->Text = $mrt->Name . " MRT";
				} 
				
				if ($foodPlaceRecord->PriceRange > 0) {
					$price = PriceRangeRecord::finder()->findByPk($foodPlaceRecord->PriceRange);				
					$this->infoPrice->Text = $price->PriceRange;
				}
				
				if ($foodPlaceRecord->Contact != "")
					$this->infoContact->Text = $foodPlaceRecord->Contact;
				else
					$this->infoContact->Text = "67336789";

				if ($foodPlaceRecord->MapGoogle != "")					
					$this->lblMap->Text = $foodPlaceRecord->MapGoogle;
				//else	
					//$this->lblMap->Text = '<iframe width="381" height="140" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=singapore+tiong+bahru+plaza&amp;aq=&amp;sll=1.397605,103.818003&amp;sspn=0.009192,0.009645&amp;g=singapore+787110&amp;ie=UTF8&amp;hq=singapore+tiong+bahru+plaza&amp;t=m&amp;ll=1.286451,103.827195&amp;spn=0.006007,0.016308&amp;z=15&amp;iwloc=A&amp;output=embed"></iframe>';
				
				if ($foodPlaceRecord->PlaceDetails != "")					
					$this->lblFoodPlaceDetails->Text = "<p>".$foodPlaceRecord->PlaceDetails."</p>";
				//else	
					//$this->lblFoodPlaceDetails->Text = "<p>Artisan bakery and food store Baker & Cook is co-owned by New Zealand TV personality and celebrity chef Dean Brettschneider, who has more than 25 years of baking experience. His store offers European breads, pizzas and cakes, as well as its own-brand range of jams, chutneys and handcrafted packaged cookies.</p>";
				
				$this->renderBoxComments();
				$this->renderBoxPhotos();
				
				if ($this->Application->User->UserName != "") 
					$this->lblUsername->Text = $this->Application->User->UserName;
				else
				{
					$this->lblUsername->Text = "Guest";									
					$this->lnkLogin2->Visible = true;
					$this->lblLine->Visible = true;
					$this->lnkRegister2->Visible = true;
				}
			}
		}
	}
	
	protected function renderBoxComments()
	{
		$limit = null;
		$offset = null;
		$sqlmap = $this->Application->Modules['sqlmap']->Client;
		$items = $sqlmap->queryForList("GetReviewsByFoodPlaceID",$this->txtFoodPlaceID->Value,null,$offset,$limit);
		$this->ListComments->DataSource = $items;
		$this->ListComments->DataBind();
		
		if (count($items) <= 0)
		{
			$this->lblTitleReview->Text="Reviews (0)";
		}	
	}
	
	protected function renderBoxPhotos()
	{
		$limit = null;
		$offset = null;
		$sqlmap = $this->Application->Modules['sqlmap']->Client;
		$items = $sqlmap->queryForList("GetPhotosByFoodPlaceID",$this->txtFoodPlaceID->Value,null,$offset,$limit);
		$this->ListPhotos->DataSource = $items;
		$this->ListPhotos->DataBind();	
	}
	
	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{
			try
			{
				$reviewRecord = new ReviewRecord;
				$reviewRecord->FoodPlaceID = $this->txtFoodPlaceID->Value;
				$reviewRecord->Review = $this->txtReview->SafeText;
				$reviewRecord->Status = 1;
				$reviewRecord->CreateBy = $reviewRecord->ModifyBy = 0; // 0: self created
				if ($this->Application->User->ID != "")
					$reviewRecord->CreateBy = $reviewRecord->ModifyBy = $this->Application->User->ID;
				$reviewRecord->CreateDate = $reviewRecord->ModifyDate = time();							
				$reviewRecord->save();				
				$this->Response->redirect($this->Service->ConstructUrl("FoodPlaceDetails", array('fid'=>$this->txtFoodPlaceID->Value, 'm'=>'cr')));										
			}
			catch(TException $e)
			{
				echo $e;
				//$this->Notice->Type = UserNoticeType::Error;
				//$this->Notice->Text = $this->Application->getModule('message')->translate('REGISTER_FAILED');
			}
		}
	}
}

?>